<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $captcha = $_POST['captcha'];
    
    // 验证验证码
    session_start();
    if ($captcha != $_SESSION['captcha']) {
        $message = "验证码错误！";
        $isError = true;
    } else {
        // 检查用户名是否已存在
        $usersData = file_get_contents('user.txt');
        $users = json_decode($usersData, true);
        $userExists = false;
        
        foreach ($users['users'] as $user) {
            if ($user['username'] === $username) {
                $userExists = true;
                break;
            }
        }
        
        if (!$userExists) {
            // 添加到待审批列表
            $pendingData = file_exists('pending_users.txt') ? file_get_contents('pending_users.txt') : '{"users":[]}';
            $pendingUsers = json_decode($pendingData, true);
            $pendingUsers['users'][] = ['username' => $username, 'password' => $password];
            file_put_contents('pending_users.txt', json_encode($pendingUsers, JSON_PRETTY_PRINT));
            $message = "注册成功，等待管理员审批！";
            $isError = false;
        } else {
            $message = "用户名已存在！";
            $isError = true;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原创软件用户管理 - 用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .register-container {
            background: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            width: 300px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            text-align: center;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .captcha-group {
            display: flex;
            gap: 10px;
            align-items: center;
        }
        .captcha-input {
            flex: 1;
        }
        .captcha-image {
            height: 30px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <h2>用户注册</h2>
        <?php if (isset($message)): ?>
            <div class="message <?php echo $isError ? 'error' : 'success'; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        <form action="" method="post">
            <div class="form-group">
                <label for="username">用户名：</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码：</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="captcha">验证码：</label>
                <div class="captcha-group">
                    <input type="text" id="captcha" name="captcha" class="captcha-input" required maxlength="4">
                    <img src="captcha.php" alt="验证码" class="captcha-image" onclick="this.src='captcha.php?'+Math.random()">
                </div>
            </div>
            <button type="submit">注册</button>
        </form>
        <p style="text-align: center; margin-top: 15px;">
            <a href="login.php">返回登录</a>
        </p>
    </div>
</body>
</html> 